Certified Java Programmer Mock Exam


Question 1


interface A {
  void m1();            // 1
  public void m2();     // 2
  protected void m3();  // 3
  private void m4();    // 4
}

What is the result of attempting to compile the code?

a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. None of the Above.

Question 2


interface A {  
  protected int e = 5;              // 1
  private int f = 6;                // 2
  volatile int g = 7;               // 3
  transient int h = 8;              // 4
}

Which of the field declarations result in a compile-time error?

a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 3



Which of the following are modifiers that can be applied to an interface that is not a member of an enclosing class?

a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. None of the above.

Question 4


interface A {
  void m1();              // 1
}
class B implements A {
  public void m1() {}     // 2
}
class C implements A {
  protected void m1() {}  // 3
}
class D implements A {
  private void m1() {}    // 4
}
class E implements A {
  void m1() {}            // 5
}

What is the result of attempting to compile the code?

a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. Compiler error at line 5.
f. None of the Above.

Question 5



Which of the following are true statements?

a. The relationship between a class and its super class is an example of a "has-a" relationship.
b. The relationship between a class and its super class is an example of an "is-a" relationship.
c. The relationship between a class and a field within the class is an example of a "has-a" relationship.
d. The relationship between a class and a field within the class is an example of an "is-a" relationship.
e. None of the Above.

Question 6


interface F {abstract void main(String[] args);}      // 1
interface G {synchronized void main(String[] args);}  // 2
interface H {final void main(String[] args);}         // 3
interface I {native void main(String[] args);}        // 4

Which interface declarations generate a compile-time error?

a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 7



Which of the following are true statements?

a. An interface that is declared within the body of a class or interface is known as a nested interface.
b. A constant can be a member of an interface.
c. An abstract method declaration can be a member of an interface.
d. A class declaration can be a member of an interface.
e. None of the above.

Question 8



Which of the following modifiers can be applied to a class that is declared within an enclosing interface?

a. public
b. protected
c. private
d. abstract
e. static
f. final
g. strictfp
h. None of the above.

Question 9



Which of the following are modifiers that can be applied to a member interface?

a. static
b. synchronized
c. transient
d. volatile
e. strictfp
f. None of the above.

Question 10


1. class A {void m1(String s1) {}}
2. class B extends A {
3.   void m1(String s1) {}
4.   void m1(boolean b) {}
5.   void m1(byte b) throws Exception {}
6.   String m1(short s) {return new String();}
7.   private void m1(char c) {}
8.   protected void m1(int i) {}
9. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Compiler error at line 8.
g. Compiler error at line 9.
h. None of the Above

Question 11



Which of the following are modifiers that can be applied to a member interface?

a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. None of the above.

Question 12



Which of the following are true statements?

a. An interface declaration can be a member of an interface.
b. A method declared within an interface must have a body represented by empty curly braces.
c. An interface can implement another interface.
d. An abstract class that implements an interface must implement all abstract methods declared in the interface.
e. None of the above.

Question 13


class R {
  private void printS1(){System.out.print("R.printS1 ");}
  protected void printS2() {System.out.print("R.printS2 ");}
  protected void printS1S2(){printS1();printS2();}
}

class S extends R {
  private void printS1(){System.out.print("S.printS1 ");}
  protected void printS2(){System.out.print("S.printS2 ");}
  public static void main(String[] args) {
    new S().printS1S2();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: R.printS1 R.printS2
b. Prints: R.printS1 S.printS2
c. Prints: S.printS1 R.printS2
d. Prints: S.printS1 S.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 14


class A {
  String s1 = "A.s1";
  String s2 = "A.s2";
}

class B extends A {
  String s1 = "B.s1";
  public static void main(String args[]) {
    B x = new B();
    A y = (A)x;
    System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: B.s1 A.s2 B.s1 A.s2
b. Prints: B.s1 A.s2 A.s1 A.s2
c. Prints: A.s1 A.s2 B.s1 A.s2
d. Prints: A.s1 A.s2 A.s1 A.s2
e. Runtime error.
f. Compiler error.
g. None of the Above

Question 15


interface Z {void m1();}  // 1
class A implements Z {void m1() {}} // 2
class B implements Z {public void m1() {}} // 3
abstract class C implements Z {public abstract void m1();} // 4

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. None of the above.

Question 16


interface A {
  String m1();
  class B implements A {public String m1() {return "B";}}
}
class C implements A {
  public String m1() {return "C";}
  class D extends B {public String m1() {return "D";}}
}
class E {
  public static void main(String[] args) {
    // Insert statement here.
  }
}

Which of the following statements could be inserted into the program to print the String ""D"?

a. System.out.print(m1());
b. System.out.print(D.m1());
c. System.out.print(C.D.m1());
d. System.out.print(new D().m1());
e. System.out.print(new C.D().m1());
f. System.out.print(new C().new D().m1());
g. Run"time Error
h. Compile"time Error
i. None of the Above

Question 17


class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
  String s1="A";
  class B extends SuperB {  // 1
    String s1="B";
    void m1() {
      System.out.print(this.s1 + ","); // 2
      System.out.print(super.s1 + ","); // 3
      System.out.print(A.this.s1 + ","); // 4
      System.out.print(A.super.s1); // 5
    }
  }
  public static void main (String[] args) {
    new A().new B().m1(); // 6
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: B,SuperB,B,SuperB
b. Prints: B,SuperB,A,SuperA
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Compiler Error at 5.
h. Compiler Error at 6.
i. Runtime Error
j. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1